
/*******************************************************************************
 * Copyright (c) 2007 Jules White. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: Jules White - initial API and implementation
 ******************************************************************************/
package org.refresh.prolog;


public class PrologAssertionManager implements AssertionManager {
	private Prolog prolog_;
	
	public void assertFact(Fact f) {
		String query = "assert("+constructQuery(f)+").";
		prolog_.query(query);
	}

	public void retractFact(Fact f) {
		String query = "retract("+constructQuery(f)+").";
		prolog_.query(query);
	}

	public static String sanitizeIdentifier(String id){
		return PiD.toPID(id);
	}
	
	public String constructQuery(Fact f){
		String query = sanitizePredicate(f.getPredicate());
		query += "(";
		Object args[] = f.getArguments();
		for(int i = 0; i < args.length; i++){
			if(i != 0)
				query += ",";
			if(args[i] instanceof ID)
			    query += PiD.toPID((ID)args[i]);
			else
				query += args[i];
		}
		query += ")";
		
		return query;
	}
	
	public String sanitizePredicate(String pred){
		if(pred.equals("is_a")){
			return pred;
		}
		else{
			return "self_"+pred;
		}
	}

	public Prolog getProlog() {
		return prolog_;
	}

	public void setProlog(Prolog prolog) {
		prolog_ = prolog;
	}
	
	
}
